//
//  SettingView.swift
//  BasicTools
//
//  Created by ikang on 2025/8/25.
//

import SwiftUI

public struct SettingView: View {
    @State public var listData: SettingDataModel
    public var onTapDone: ((_ sectionIndex: Int, _ itemIndex: Int) -> Void)?
    
    public init(listData: SettingDataModel, 
                onTapDone: ((_ sectionIndex: Int, _ itemIndex: Int) -> Void)? = {_, _ in }) {
        self.listData = listData
        self.onTapDone = onTapDone
    }
    
    public var body: some View {
        
        ScrollView() {
            LazyVStack(spacing: SettingModel.shared.sectionSpace) { 
                ForEach(0..<listData.sectionArr.count, id: \.self) { sectionIndex in
                    
                    var sectionModel = listData.sectionArr[sectionIndex]
                    
                    VStack(alignment: .leading, spacing: 5) {
                        // HeaderView
                        if !sectionModel.headerTitle.isEmpty {
                            HStack { 
                                Text(sectionModel.headerTitle)
                                    .font(SettingModel.shared.headerTitleFont)
                                    .foregroundStyle(SettingModel.shared.headerTitleColor)
                            }
                            .padding(SettingModel.shared.headerPadding)
                        }
                        
                        // ItemCell
                        VStack(spacing: 0) {
                            ForEach(0..<sectionModel.itemArr.count) { itemIndex in
                                @Bindable var itemModel = sectionModel.itemArr[itemIndex]
                                if itemModel.type == .titleHStack || itemModel.type == .titleVStack {
                                    SettingArrowView(itemModel: itemModel, isShowLine: itemIndex != sectionModel.itemArr.count - 1)
                                        .onTapGesture {
                                            if let onTapDone {
                                                onTapDone(sectionIndex, itemIndex)
                                            }
                                        }
                                }else if itemModel.type == .toggle {
                                    SettingToggleSView(itemModel: itemModel, isShowLine: itemIndex != sectionModel.itemArr.count - 1)
                                }
                            }
                        }
                        .background(SettingModel.shared.sectionBackgroundColor)
                        .cornerRadius(SettingModel.shared.sectionCornerRadius)   
                    }
                }
            }
            .padding(SettingModel.shared.listPadding)
            
        }
        .background(SettingModel.shared.listBackgroundColor)
    }
}

//#Preview {
//    SettingView()
//}
